package com.joysuch.wwyt.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.edu.entity.WwytEduCertificate;
import com.joysuch.wwyt.edu.mapper.WwytEduCertificateMapper;
import com.joysuch.wwyt.edu.repository.EduCertificateDao;
import com.joysuch.wwyt.edu.service.WwytEduCertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author : pp
 * @date : Created in 2021/4/14 20:40
 */
@Service
public class WwytEduCertificateServiceImpl extends ServiceImpl<WwytEduCertificateMapper, WwytEduCertificate> implements WwytEduCertificateService {
    @Autowired
    private EduCertificateDao eduCertificateDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private WwytEduCertificateMapper eduCertificateMapper;
    @Autowired
    private BaseTypeGroupItemDao itemDao;

    @Override
    public void add(WwytEduCertificate entity) {
        //校验名称重复
        LambdaQueryWrapper<WwytEduCertificate> repeat = new QueryWrapper<WwytEduCertificate>().lambda()
                .eq(WwytEduCertificate::getName, entity.getName())
                .eq(WwytEduCertificate::getTenentId, Context.getCurrentTenantId())
                .likeRight(WwytEduCertificate::getOrgCode, Context.getCompanyCode());
        Integer count = eduCertificateMapper.selectCount(repeat);
        if (count > 0) {
            throw new IllegalArgumentException("名称 " + entity.getName() + "已存在");
        }

//        //字典码处理
//        if (StringUtils.isNotBlank(entity.getCode())) {
//            Assert.isTrue(eduCertificateDao.countOfCode(entity.getCode()) == 0, String.format("编号%s已经存在", entity.getCode()));
//        } else {
//            entity.setCode(bizCodeGeneratorFacade.zhengShu());
//        }

        Optional<BaseTypeGroupItem> byId = itemDao.findById(entity.getCertTypeId());
        if (byId.isPresent()) {
            entity.setCode(byId.get().getCode());
        } else {
            throw new IllegalArgumentException("字典表查找不到对应证书字典数据");
        }

        save(entity);
    }

    @Override
    public void update(WwytEduCertificate entity) {
        //校验名称重复
        LambdaQueryWrapper<WwytEduCertificate> repeat = new QueryWrapper<WwytEduCertificate>().lambda()
                .ne(WwytEduCertificate::getId, entity.getId())
                .eq(WwytEduCertificate::getName, entity.getName())
                .eq(WwytEduCertificate::getTenentId, Context.getCurrentTenantId())
                .likeRight(WwytEduCertificate::getOrgCode, Context.getCompanyCode());
        Integer count = eduCertificateMapper.selectCount(repeat);
        if (count > 0) {
            throw new IllegalArgumentException("名称 " + entity.getName() + "已存在");
        }

        Optional<BaseTypeGroupItem> byId = itemDao.findById(entity.getCertTypeId());
        if (byId.isPresent()) {
            entity.setCode(byId.get().getCode());
        } else {
            throw new IllegalArgumentException("字典表查找不到对应证书字典数据");
        }


        saveOrUpdate(entity);
    }

    @Override
    public WwytEduCertificate selectByCertificateName(String certificateName) {
        LambdaQueryWrapper<WwytEduCertificate> eq = new QueryWrapper<WwytEduCertificate>().lambda()
                .eq(WwytEduCertificate::getName, certificateName)
                .eq(WwytEduCertificate::getTenentId, Context.getCurrentTenantId())
                .likeRight(WwytEduCertificate::getOrgCode, Context.getCompanyCode());
        return eduCertificateMapper.selectOne(eq);
    }
}
